unit uObjectTimestamp;

interface

uses
  Windows, Classes, Contnrs;

type
  TObjectTimestampListItem = class;
  TObjectTimestampList = class;
  TObjectTimestampListItem = class(TCollectionItem)
  private
    FBoundObject: TObject;
    FTimestamp: Cardinal;
    function GetExistentTime: Cardinal;
  public
    property BoundObject: TObject read FBoundObject write FBoundObject;
    property ExistentTime: Cardinal read GetExistentTime;
    property Timestamp: Cardinal read FTimestamp write FTimestamp;
  end;

  TObjectTimestampList = class(TCollection)
  private
    function GetObject(Index: Integer): TObject;
  protected
    function Add: TObjectTimestampListItem;
    property Items;
  public
    procedure AddObject(AObject: TObject);
    function RemoveFirst: TObject;
    property Objects[Index: Integer]: TObject read GetObject;
  end;

implementation



{ TObjectTimestampListItem }

{
*************************** TObjectTimestampListItem ***************************
}
function TObjectTimestampListItem.GetExistentTime: Cardinal;
begin
  Result:= GetTickCount - Timestamp;
end;

{ TObjectTimestampList }

{
***************************** TObjectTimestampList *****************************
}
function TObjectTimestampList.Add: TObjectTimestampListItem;
begin
  Result:= TObjectTimestampListItem(inherited Add);
end;

procedure TObjectTimestampList.AddObject(AObject: TObject);
begin
  with Add do
  begin
    BoundObject:= AObject;
    Timestamp:= GetTickCount;
  end;
end;

function TObjectTimestampList.GetObject(Index: Integer): TObject;
begin
  Result:= TObjectTimestampListItem(Items[Index]).BoundObject;
end;

function TObjectTimestampList.RemoveFirst: TObject;
begin
  Result:= nil;
  if Count > 0 then
  begin
    Result:= Objects[0];
    Delete(0);
  end;
end;

end.
